package org.chenbingkang.boot.service.impl;


import com.aliyun.oss.OSS;
import com.aliyun.oss.model.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.chenbingkang.boot.common.VODUtil;
import org.chenbingkang.boot.pojo.*;
import org.chenbingkang.boot.service.VODService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@DubboService(version = "1.0.0")
public class OSSVODServiceImpl implements VODService {
    private static Logger logger = LoggerFactory.getLogger(OSSVODServiceImpl.class);

    @Autowired
    private OSS ossClient;

    @Value("${oss.bucket.vod}")
    private String bucketName;

    @Value("${tmp-vod-file-dir}")
    private String tmpDir;

    @Override
    public EditAudioResponse editAudio(EditAudioRequest request) {
        EditAudioResponse response = new EditAudioResponse();
        try {
            String path = request.getPath();
            if (StringUtils.isEmpty(path)) {
                //TODO
            }
            String start = request.getStart();
            Integer duration = request.getDuration();
            String localFile = getOSSFile(path);
            String resultFile = tmpDir + File.separator + UUID.randomUUID().toString().replace("-", "");
            boolean result = VODUtil.editVOD(localFile, resultFile, start, duration);
            if (result) {
                String resultPath = putOSSFile(resultFile);
                response.setPath(resultPath);
            }
        } catch (Exception e) {
            logger.error("剪辑音频处理失败", e);
        }
        return response;
    }

    @Override
    public EditVideoResponse editVideo(EditVideoRequest request) {
        EditVideoResponse response=new EditVideoResponse();
        try {
            String path=request.getPath();
            String start = request.getStart();
            Integer duration = request.getDuration();
            Integer x=request.getX();
            Integer y=request.getY();
            Integer width=request.getWidth();
            Integer height=request.getHeight();
            String localFile = getOSSFile(path);
            String resultFile = tmpDir + File.separator + UUID.randomUUID().toString().replace("-", "");
            boolean result=false;
            if(x!=null&&StringUtils.isNotEmpty(start)){
               result = VODUtil.cutAndEditVDO(localFile, resultFile, start, duration,x,y,width,height);
            }else if(x==null){
                result = VODUtil.editVOD(localFile,resultFile,start,duration);
            }else {
                result = VODUtil.cutVDO(localFile,resultFile,x,y,width,height);
            }
            if (result) {
                String resultPath = putOSSFile(resultFile);
                response.setPath(resultPath);
            }
        } catch (Exception e) {
            logger.error("剪辑视频处理失败", e);
        }
        return response;
    }

    @Override
    public CoverSnapshotVideoResponse coverSnapshotVideo(CoverSnapshotVideoRequest request) {
        CoverSnapshotVideoResponse response=new CoverSnapshotVideoResponse();
        try {
            String path=request.getPath();
            String localFile = getOSSFile(path);
            String resultFile = tmpDir + File.separator + UUID.randomUUID().toString().replace("-", "");
            boolean result=VODUtil.snapshotVOD(localFile, resultFile, "00:00:00", 1);
            if (result) {
                String resultPath = putOSSFile(resultFile);
                response.setPath(resultPath);
            }
        } catch (Exception e) {
            logger.error("生成视频封面失败", e);
        }
        return response;
    }

    @Override
    public SpriteSnapshotVideoResponse spriteSnapshotVideo(SpriteSnapshotVideoRequest request) {
        SpriteSnapshotVideoResponse response=new SpriteSnapshotVideoResponse();
        try {
            String path=request.getPath();
            String localFile = getOSSFile(path);
            String resultFile = tmpDir + File.separator + UUID.randomUUID().toString().replace("-", "");
            boolean result=VODUtil.spriteSnapshotVOD(localFile, resultFile);
            if (result) {
                String resultPath = putOSSFile(resultFile);
                response.setPath(resultPath);
            }
        } catch (Exception e) {
            logger.error("生成雪碧截图失败", e);
        }
        return response;
    }

    private String getOSSFile(String objectname){
        String localFile = tmpDir + File.separator + UUID.randomUUID().toString().replace("-", "");
        ossClient.getObject(new GetObjectRequest(bucketName, objectname), new File(localFile));
        return localFile;
    }

    private String putOSSFile(String localFileName)throws IOException{
        String objectname = UUID.randomUUID().toString().replace("-", "");
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectname, new FileInputStream(localFileName));
        ossClient.putObject(putObjectRequest);
        return objectname;
    }
}
